const app = getApp();
Page({
    data: {
        gid: '',
        navigationBar: 0,
        dataSite: {},
        showStartSiteList: false,
        showEndSiteList: false,
        startSiteSelect: '',
        endSiteSelect: '',
        startSiteSelectInfo: {},
        endSiteSelectInfo: {},
        priceInfo: {},
        isLoading: false,
    },

    onLoad: function (options) {
        const eventChannel = this.getOpenerEventChannel();
        eventChannel.on('acceptDataFromHomePage', (data) => {
            const {gid} = data;
            const systemInfo = wx.getSystemInfoSync();
            const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
            this.setData({
                gid: gid,
                navigationBar: (menuButtonInfo.top - (systemInfo?.statusBarHeight || 0)) * 2 + menuButtonInfo.height,
            });
            this.queryDetail();
        });
    },


    queryDetail: async function () {
        try {
            this.setData({isLoading: true});
            const res = await app.request({
                url: '/sitebus/line/get_line_class_day_detail',
                data: {
                    "gid": this.data.gid,
                }
            });
            let startInfo = res.data.start_station_list[0];
            let endInfo = res.data.end_station_list[0];
            let startGid = startInfo.NodeGID;
            let endGid = endInfo.NodeGID;
            let priceInfo = res.data.price_list.find(item =>
                item.StartNodeGID === startGid && item.ArrivalNodeGID === endGid
            );
            this.setData({
                dataSite: res.data,
                startSiteSelect: startGid,
                startSiteSelectInfo: startInfo,
                endSiteSelect: endGid,
                priceInfo: priceInfo,
                endSiteSelectInfo: endInfo,
            });

        } catch (err) {
            wx.showToast({
                title: err.message || '加载失败',
                icon: 'none'
            });
        } finally {
            this.setData({isLoading: false});
        }
    },

    onShowStartSiteList: function () {
        this.setData({
            showStartSiteList: true
        })
    },
    onShowEndSiteList: function () {
        this.setData({
            showEndSiteList: true
        })
    },

    onClickStart: function (event) {
        let stationInfo = this.data.dataSite.start_station_list.find(item =>
            item.NodeGID === event.detail
        );
        let priceInfo = this.data.dataSite.price_list.find(item =>
            item.StartNodeGID === event.detail && item.ArrivalNodeGID === this.data.endSiteSelect
        );
        this.setData({
            startSiteSelectInfo: stationInfo,
            startSiteSelect: event.detail,
            priceInfo: priceInfo,
        })
    },

    onStartItemClick: function (event) {
        const stationInfo = event.currentTarget.dataset.date;
        let startNodeGID = stationInfo.NodeGID;

        let priceInfo = this.data.dataSite.price_list.find(item =>
            item.StartNodeGID === startNodeGID && item.ArrivalNodeGID === this.data.endSiteSelect
        );
        this.setData({
            startSiteSelectInfo: stationInfo,
            startSiteSelect: startNodeGID,
            priceInfo: priceInfo,
        })
    },

    onClickEnd: function (event) {
        let stationInfo = this.data.dataSite.end_station_list.find(item =>
            item.NodeGID === event.detail
        );
        let priceInfo = this.data.dataSite.price_list.find(item =>
            item.StartNodeGID === this.data.startSiteSelect && item.ArrivalNodeGID === event.detail
        );
        console.log("priceInfo", priceInfo)
        this.setData({
            endSiteSelectInfo: stationInfo,
            endSiteSelect: event.detail,
            priceInfo: priceInfo,
        })
    },

    onClickButton: function () {
        wx.navigateTo({
            url: `/pages/sitebus/place_order/place_order`,
            // 使用箭头函数确保 this 指向正确
            success: (res) => {
                res.eventChannel.emit('acceptDataFromOpenerPage', {
                    dataSite: this.data.dataSite,
                    startSiteSelectInfo: this.data.startSiteSelectInfo,
                    endSiteSelectInfo: this.data.endSiteSelectInfo,
                    priceInfo: this.data.priceInfo,
                })
            }
        });
    },
});